<?php
namespace app\banji\controller;

use app\common\model\BanjiModel;
use cmf\controller\AdminBaseController;
use app\common\model\VideoModel;

class BanjiController extends AdminBaseController
{
    public function index()
    {
        $name           = input('name');
        $where          = [];
        if ($name) {
            $where[]          = ['name', 'like', $name.'%'];
        }
        $total_count = BanjiModel::where($where)->count();
        $list        = BanjiModel::where($where)
            ->order('id DESC')
            ->paginate(page_config(), $total_count);

        $this->assign([
            'list'             => $list,
            'page'             => $list->render(),
            'name'             => $name,
        ]);

        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $input = input();

            $insert['name']          = $input['name'];

            //保存数据
            if ($input['id']) {
                $where['id'] = $input['id'];
                $result      = BanjiModel::update($insert, $where);
            } else {
                $insert['create_time'] = time();
                $result = BanjiModel::create($insert);
            }

            if (empty($result)) {
                $this->error();
            }
            $this->success('保存成功', 'index');
        }
        $id = input('id');
        if (empty($id)) {
            $channel = BanjiModel::getFields();
        } else {
            $channel = BanjiModel::get($id);
            if (!$channel) {
                $this->error('数据不存在');
            }
        }

        $this->assign([
            'list'    => $channel,
        ]);
        return $this->fetch();
    }

    public function delete(){
        $id             = $this->request->param('id');
        $check = VideoModel::where(['class_id'=>$id])->find();
        if($check){
            $this->error('存在关联数据不能删除哦！', '');
        }
        BanjiModel::where(['id'=>$id])->delete();
        $msg =  '删除成功';
        $this->success($msg, '');
    }
}